    program find_episodes
    character*6 ticker(5000)
    character*8 cusp(5000)
    character*9 temp9
    integer(kind=8) nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
    dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
    dimension  price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000)
!      open(unit=10,file='e:\FIS\crsp_util_sorted_fees_2005_2023.txt')
!      open(unit=98,file='e:\FIS_duration\episode_data.txt')
!      open(unit=99,file='e:\FIS_duration\episode_results.txt')
      open(unit=10,file='e:\FIS\crsp_util_sorted_fees_2005_2023_rep.txt')
      open(unit=98,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\episode_results_rep.txt')
      iend=0
      nep=0
      i=0
1     i=i+1      
2     read(10,1000,end=900)jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),jvalue(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i),nvalue(i)      
1000  format(2(1x,i8),1x,a8,2x,a6,4x,i9,2(1x,i14),5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f9.6,1x,i9,1x,f9.6,1x,f6.2,2(1x,i13)) 
      if(retrn(i).lt.-1.) go to 2
!      dfee(i)=favg(i)/25200.
      dfee(i)=((1.+(favg(i)/100.))**.0039683)-1.
      if(dfee(i).lt.0.) dfee(i)=0.
!      write(6,6000)favg(i),dfee(i)
!6000  format(2(1x,f11.7))      
      if(jast(i).eq.jast(1)) go to 1
5     icnt=i-1
      if(icnt.lt.2) go to 500
      iep=0
      j=1
10    j=j+1
      if(j.gt.icnt) go to 400
      jm=j-1
      if(iep.eq.1) go to 100
      if(favg(j).ge.25.and.favg(jm).ge.25.and.price(j).ge.5.) go to 20
!      if(favg(j).ge.20.and.favg(jm).ge.20.and.price(j).ge.5.) go to 20
      go to 10
20    iep=1
      jbg=jm
      nep=nep+1
!      tret=(1.+retrn(j))*(1.+retrn(jm))
!      xsret=(1.+retrn(j)-vwret(j))*(1.+retrn(jm)-vwret(jm))
!      fret=(1.+retrn(j)+dfee(j))*(1.+retrn(jm)+dfee(jm))
!      fxsret=(1.+retrn(j)-vwret(j)+dfee(j))*(1.+retrn(jm)-vwret(jm)+dfee(jm))
      tret=1.
      xsret=1.
      fret=1.
      fxsret=1.
      retmin=9.99
      retmin2=8.88
      mindate=0
      mindate2=0
      retmax=-9.90
      retmax2=-8.88
      maxdate=0
      maxdate2=0
      
!      write(98,9800)nep,jast(jm),jdate(jm),cusp(jm),ticker(jm),itickt(jm),numshrs(jm),age(jm),favg(jm),fmax(jm),fmin(jm),fstd(jm),iperm(jm),price(jm),ivol(jm),retrn(jm),ishrout(jm),vwret(jm),util(jm),nshrs(jm)     
!      write(98,9800)nep,jast(j),jdate(j),cusp(j),ticker(j),itickt(j),numshrs(j),age(j),favg(j),fmax(j),fmin(j),fstd(j),iperm(j),price(j),ivol(j),retrn(j),ishrout(j),vwret(j),util(j),nshrs(j)     
9800  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13) 
      go to 10
100   continue  
      tret=tret*(1+retrn(j))
      xsret=xsret*(1+retrn(j)-vwret(j))
      fret=fret*(1+retrn(j)+dfee(j))
      fxsret=fxsret*(1+retrn(j)-vwret(j)+dfee(j))
      if(retrn(j).lt.retmin.and.retrn(j).lt.retmin2) go to 110
      if(retrn(j).ge.retmin.and.retrn(j).lt.retmin2) go to 120
      if(retrn(j).gt.retmax.and.retrn(j).gt.retmax2) go to 130
      if(retrn(j).le.retmax.and.retrn(j).gt.retmax2) go to 140
      go to 150
110   continue
      mindate2=mindate
      mindate=jdate(j)
      retmin2=retmin
      retmin=retrn(j)
      go to 150
120   continue
      mindate2=jdate(j)
      retmin2=retrn(j)
      go to 150
130   continue
      maxdate2=maxdate
      retmax2=retmax
      maxdate=jdate(j)
      retmax=retrn(j)
      go to 150
140   continue
      maxdate2=jdate(j)
      retmax2=retrn(j)
150   continue      
!150   write(98,9800)nep,jast(j),jdate(j),cusp(j),ticker(j),itickt(j),numshrs(j),age(j),favg(j),fmax(j),fmin(j),fstd(j),iperm(j),price(j),ivol(j),retrn(j),ishrout(j),vwret(j),util(j),nshrs(j)     
      if(favg(j).ge.2.or.favg(jm).ge.2) go to 10
!      if(favg(j).ge.1.or.favg(jm).ge.1) go to 10
      iep=0
      jend=j
      tret=tret-1
      xsret=xsret-1
      fret=fret-1
      fxsret=fxsret-1
      nday=jend-jbg
!!
      rday=float(nday)
      dayret=(1.+tret)**(1./rday)-1
      dayxs=(1.+xsret)**(1./rday)-1
      annret=(1.+tret)**(252./rday)-1
      annxs=(1.+xsret)**(252./rday)-1
!!
      ntype=1
      write(99,9900)nep,ntype,jast(jbg),jdate(jbg),cusp(jbg),ticker(jbg),nday,tret,xsret,fret,fxsret,retmin,mindate,retmin2,mindate2,retmax2,maxdate2,retmax,maxdate,dayret,dayxs,annret,annxs
      write(6,9900)nep,ntype,jast(jbg),jdate(jbg),cusp(jbg),ticker(jbg),nday,tret,xsret,fret,fxsret,retmin,mindate,retmin2,mindate2,retmax2,maxdate2,retmax,maxdate,dayret,dayxs,annret,annxs
9900  format(1x,i4,1x,i1,1x,i8,1x,i8,1x,a8,1x,a6,1x,i4,4(1x,f12.6),4(1x,f9.6,1x,i8),4(1x,f11.6)) 
      go to 10
400   continue
      if(iep.eq.0) go to 500
      jend=j
      tret=tret-1
      xsret=xsret-1
      fret=fret-1
      fxsret=fxsret-1
      nday=jend-jbg
!!
      rday=float(nday)
      dayret=(1.+tret)**(1./rday)-1
      dayxs=(1.+xsret)**(1./rday)-1
      annret=(1.+tret)**(252./rday)-1
      annxs=(1.+xsret)**(252./rday)-1
!!
      ntype=2
      if(jdate(icnt).eq.20231229) ntype=3
      write(99,9900)nep,ntype,jast(jbg),jdate(jbg),cusp(jbg),ticker(jbg),nday,tret,xsret,fret,fxsret,retmin,mindate,retmin2,mindate2,retmax2,maxdate2,retmax,maxdate,dayret,dayxs,annret,annxs
      write(6,9900)nep,ntype,jast(jbg),jdate(jbg),cusp(jbg),ticker(jbg),nday,tret,xsret,fret,fxsret,retmin,mindate,retmin2,mindate2,retmax2,maxdate2,retmax,maxdate,dayret,dayxs,annret,annxs
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      jvalue(1)=jvalue(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      nvalue(1)=nvalue(i) 
      dfee(1)=dfee(i)
      i=1
      go to 1
900   iend=iend+1
      if(iend.eq.1) go to 5 
      stop

    end program find_episodes
